package lumingweihua.future.cn.lumingweihua.ui.message;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.zhusx.core.adapter.Lib_BaseRecyclerAdapter;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialog;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialogEntity;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.TransportationStatusData;

/**
 * Author       zhusx
 * Created      2018/3/16 9:46
 */

public class StatusActivity extends BaseCompatActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    LoadData<String> clearData;
    Lib_BaseRecyclerAdapter<TransportationStatusData> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_status);
        ButterKnife.bind(this);
        clearData = new LoadData<>(LoadData.Api.消息运输动态列表清空, this);
        clearData._setOnLoadingListener(new SimpleHttpListener<String>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<String> result) {
                adapter._clearItemToUpdate();
                showToast(result.getData());
                setResult(RESULT_OK);
            }
        });
        LoadData<List<TransportationStatusData>> loadData = new LoadData<>(LoadData.Api.消息运输动态列表, this);
        loadData._setOnLoadingListener(new LoadingHelper<List<TransportationStatusData>>(recyclerView, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<List<TransportationStatusData>> data) {
                init(data.getData());
            }
        });
        loadData._loadData();
    }

    private void init(List<TransportationStatusData> data) {
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(0, 2, 0, 0);
            }
        });
        recyclerView.setAdapter(adapter = new Lib_BaseRecyclerAdapter<TransportationStatusData>(data) {
            @Override
            protected void __bindViewHolder(_ViewHolder viewHolder, int i, final TransportationStatusData s) {
                viewHolder.setText(+R.id.tv_no, "货单号: " + s.good_num);
                viewHolder.setText(R.id.tv_name, String.format("[%s  %s]", s.license, s.car_name));
                viewHolder.setText(R.id.tv_message, s.dynamic);
                if (!TextUtils.isEmpty(s.create_time)) {
                    viewHolder.setText(R.id.tv_date, s.create_time.replace(" ", "\n"));
                }
            }

            @Override
            protected int __getLayoutResource(int i) {
                return R.layout.item_list_notify_yun;
            }
        });
    }

    @OnClick(R.id.tv_titleBar_right)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_titleBar_right:
                new YesOrNoDialog(v.getContext(), new YesOrNoDialogEntity("是否清空运输消息?", "取消", "清空"),
                        new YesOrNoDialog.OnYesOrNoDialogClickListener() {
                            @Override
                            public void onYesOrNoDialogClick(YesOrNoDialog.YesOrNoType yesOrNoType) {
                                switch (yesOrNoType) {
                                    case BtnOk:
                                        clearData._loadData();
                                        break;

                                    default:
                                        break;
                                }
                            }
                        }).show();
                break;
        }
    }
}
